package br.com.iswe.gwt.ui.client.service;

import java.util.List;

import br.com.iswe.core.entidades.gwt.BeanMap;
import br.com.iswe.gwt.ui.client.ClientInfoContext;
import br.com.iswe.gwt.ui.client.CommonConstants;
import br.com.iswe.gwt.ui.client.ListBoxInfo;
import br.com.iswe.gwt.ui.client.QueryInfo;
import br.com.iswe.gwt.ui.client.RequestInfo;
import br.com.iswe.gwt.ui.client.SessionInfo;

import com.google.gwt.core.client.GWT;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;

@SuppressWarnings("unchecked")
public class BeanMapServiceClientImpl extends ServiceClientAbstract<BeanMap> {

	//protected final ManterServiceAsync manterService = (ManterServiceAsync)GWT.create(ManterService.class);

	protected static final CommonConstants commonConstants = (CommonConstants) GWT.create(CommonConstants.class);
	/**
	 * ManterServiceAsync : Servico Manter
	 */
	protected BeanMapServiceAsync serviceAsync;
	
	public BeanMapServiceClientImpl(String controllerName) {
		super(controllerName);
		this.serviceAsync = (BeanMapServiceAsync)GWT.create(BeanMapService.class); 
		((ServiceDefTarget)serviceAsync).setServiceEntryPoint(GWT.getModuleBaseURL() + "beanMapServiceImpl");
	}
	
	public BeanMapServiceAsync getServiceAsync() {
		return serviceAsync;
	}
	
	public ManyToManyClientAsyncContract getManyToManyClientAsyncContract(){
		return serviceAsync;
	}
	
	public void consultar(List<ListBoxInfo> listaRequest, AsyncCallback<List<ListBoxInfo>> callback){
		
		try{
			//TODO:Verifica, caso ainda nao existe clientInfo e ja tenha sido requisitado
			//deve esperar para realizar os proximos requests.
			RequestInfo requestInfo = new RequestInfo(ClientInfoContext.getClientInfoContext().getSessionInfo());
			serviceAsync.consultar(listaRequest, 
					requestInfo, 
					getCallBack(callback));
			
		//try catch addicionado por conta de erro da edicao da tela no GWT Designer
		}
		catch(Exception exp){
			exp.printStackTrace();
		}
		
	}
	
	public void executeList(String methodName, BeanMap beanMap, AsyncCallback<List<BeanMap>> callback) {
		RequestInfo requestInfo = new RequestInfo(ClientInfoContext.getClientInfoContext().getSessionInfo());
		serviceAsync.executeList(this.controllerName,  
				methodName, 
				beanMap, 
				requestInfo,
				getCallBack(callback));
	}
	public void executeMap(String methodName, BeanMap beanMap, AsyncCallback<BeanMap> callback) {
		RequestInfo requestInfo = new RequestInfo(ClientInfoContext.getClientInfoContext().getSessionInfo());
		serviceAsync.executeMap(this.controllerName,  
				methodName, 
				beanMap, 
				requestInfo,
				getCallBack(callback));
	}
	
	public void consultar(BeanMap beanMap, QueryInfo queryInfo, AsyncCallback<List<BeanMap>> callback){
		queryInfo.setup(ClientInfoContext.getClientInfoContext().getSessionInfo());
		serviceAsync.consultar(this.controllerName, 
				beanMap, 
				queryInfo, 
				getCallBack(callback));
	}
	
	public void detalhar(BeanMap beanMap, QueryInfo queryInfo, AsyncCallback<BeanMap> callback){
		queryInfo.setup(ClientInfoContext.getClientInfoContext().getSessionInfo());
		
		serviceAsync.detalhar(this.controllerName, 
				beanMap, 
				queryInfo, 
				getCallBack(callback));
	}
	
	public void salvar(BeanMap beanMap, AsyncCallback<BeanMap> callback){
		salvar(beanMap, callback, null);
	}
	public void salvar(BeanMap beanMap, AsyncCallback<BeanMap> callback, RequestInfo requestInfo){
		if(requestInfo == null){
			requestInfo = new RequestInfo();
		}
		requestInfo.setup(ClientInfoContext.getClientInfoContext().getSessionInfo());
		
		serviceAsync.salvar(this.controllerName, 
				beanMap,
				requestInfo, 
				getCallBack(callback));
	}
	
	public void deletar(List<BeanMap> itens, AsyncCallback<BeanMap> callback) {
		RequestInfo requestInfo = new RequestInfo(ClientInfoContext.getClientInfoContext().getSessionInfo());
		serviceAsync.deletar(this.controllerName, 
				itens,
				requestInfo, 
				getCallBack(callback));
	}
	
	public void deletar(BeanMap bean, AsyncCallback<BeanMap> callback){
		RequestInfo requestInfo = new RequestInfo(ClientInfoContext.getClientInfoContext().getSessionInfo());
		serviceAsync.deletar(this.controllerName, 
				bean,
				requestInfo, 
				getCallBack(callback));
	}

	public String getControllerName() {
		return controllerName;
	}
	
	//Controle se o sessionInfo ja foi retornado do servidor
	public void getSessionInfo(AsyncCallback<SessionInfo> sessionInfoCallBack) {
		serviceAsync.getSessionInfo(controllerName, sessionInfoCallBack);	
	}
	
}
